package coint.btc.service.auth.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import coint.btc.dto.PermissionDTO;
import coint.btc.jpa.JuesePermissionJpa;
import coint.btc.jpa.PermissionJpa;
import coint.btc.jpa.jueseJpa;
import coint.btc.pojo.Permission;
import coint.btc.service.auth.PermissionService;
import coint.btc.util.HanyuPinyinHelper;
import coint.btc.vo.Menu;
import coint.btc.vo.Page;
import coint.btc.vo.PermissionVo;

@Service
public class PermissionServiceImpl implements PermissionService  {

	
	@Autowired
	private PermissionJpa  quanxianjpa;
	
	@Autowired
	private jueseJpa  roleJpa;
	
	@Autowired
	private JuesePermissionJpa  juesequanxianjpa;


	@Override
	public boolean addJueseQuanxian(Integer jueseid, Integer[] quanxians) {
			if(jueseid==null) {
				return false;
			}
			if(quanxians.length<=0) {
				return false;
			}
			try {
				juesequanxianjpa.deleteJueseQuanXianByJueseID(jueseid);
				juesequanxianjpa.distributinQuanxian(jueseid, quanxians);
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			return true;
		
	}

	@Override
	public int addQuanXian(PermissionDTO quanxian) {
		Permission  quanbean = new Permission();
		BeanUtils.copyProperties(quanxian, quanbean);
	
		int result =quanxianjpa.addQuanXian(quanbean);
		return result;
	}

	@Override
	public boolean check(PermissionDTO quanxian) {
		boolean  flag  = quanxianjpa.CheckDouble(quanxian.getName());
		return flag;
	}

	@Override
	public List<Menu> menu() {
		List<Permission> authList=quanxianjpa.loadQuanXianTree();
		return processQuanxianList(authList, 0) ;
	}
	
	
	
	/**
	 * 
	 * @Description: 加载权限列表
	 * @date:2018年7月18日   
	 * @author: lt
	 * @param list   权限列表
	 * @param superior  父级权限id
	 * @return
	 */
	public List<Menu> processQuanxianList(List<Permission> list, int superior) {
		List<Menu> treeAuthList = new ArrayList<>();
		list.stream().filter(auth -> auth.getMgrId().intValue() == superior).forEach(auth -> {
			Menu vo = new Menu();
			
			vo.setName(HanyuPinyinHelper.hanyuToTonePinYin(auth.getName()));
			vo.setTitle(auth.getName());
			if (auth.getType().intValue() == 2) {
				vo.setJump(auth.getQxUrl());
			}
			vo.setList(processQuanxianList(list, auth.getId()));
			treeAuthList.add(vo);
		});
		HashSet<Menu> h=new HashSet<Menu>(treeAuthList);
		treeAuthList.clear();   
		treeAuthList.addAll(h); 
		return treeAuthList;
	}

	@Override
	public int updatePermission(PermissionDTO p) {
		Permission s = new Permission();
		
		try {
			if(p!=null) {
				BeanUtils.copyProperties(p, s);
			}
			quanxianjpa.updateDaozhaQuanxian(s);
			return 1;
		} catch (Exception e) {
			return  0;
		}
		
		
	}

	@Override
	public int deletePerMission(Integer id) {
		
		return quanxianjpa.deletePermission(id);
	}

	@Override
	public Page<PermissionVo> page(PermissionDTO v, Page<PermissionVo> parms) {
		
		return quanxianjpa.Page(v, parms);
	}
	
	
	

}
